package com.liu.community.server.impl;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 16:12 2021/10/12
 */

import com.liu.community.dto.CommentCreateDto;
import com.liu.community.dto.CommentDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.enums.CommentTypeEnum;
import com.liu.community.enums.NotificationStatusEnum;
import com.liu.community.enums.NotificationTypeEnum;
import com.liu.community.exception.CustomerException;
import com.liu.community.exception.ErrorCode;
import com.liu.community.mapper.*;
import com.liu.community.pojor.*;
import com.liu.community.server.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NotificationMapper notificationMapper;
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public void insert(Comment comment,User commentor ) {
            //获取评论问题的评论id

        if (comment.getParentId() == null || comment.getParentId()==0){
            throw  new CustomerException(ErrorCode.COMMENT_PARAM_NOT_FOUND);
        }
        Integer parentId =  comment.getParentId();
        if (comment.getType() == null || !CommentTypeEnum.isExist(comment.getType())){
            throw  new CustomerException(ErrorCode.TYPE_PARAM_ERROR);
        }
        if (comment.getType() == CommentTypeEnum.COMMENT.getType()){
            //回复评论
            List<Comment> dbComment = commentMapper.queryCommentByParentId(parentId);
            if (dbComment.size() == 0){
                throw  new CustomerException(ErrorCode.COMMENT_NOT_FOUND);
            }
            //回复，添加信息
            commentMapper.insert(comment);
            //修改评论数，根据parentId
             commentMapper.updateCommentCount(parentId, 1);
            //通知
            Notification notification = new Notification();
            notification.setGmtCreate(System.currentTimeMillis());
            notification.setType(NotificationTypeEnum.REPLY_COMMENT.getType());
            notification.setOuterId(parentId);
            notification.setNotifier(comment.getCommenter());
            notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
            Comment commenter = commentMapper.queryCommenter(comment.getGmtCreate());
            notification.setReceiver(commenter.getCommenter());
            notification.setNotifiName(commentor.getName());
            notification.setOuterTitle(comment.getContent());
            notificationMapper.insert(notification);
        }else if (comment.getType() == CommentTypeEnum.QUESTION.getType()){
            //回复问题
            Question question = questionMapper.queryQuestionById(parentId);
            if (question == null){
                throw  new CustomerException(ErrorCode.QUESTION_NOT_FOUND);
            }
            commentMapper.insert(comment);
            questionMapper.updateCommentCount(comment.getParentId(),1);
            //通知
            if (question.getCreator() == comment.getCommenter()){
                return;
            }
            Notification notification = new Notification();
            notification.setGmtCreate(System.currentTimeMillis());
            notification.setType(NotificationTypeEnum.REPLY_QUESTION.getType());
            notification.setOuterId(question.getId());
            notification.setNotifier(comment.getCommenter());
            notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
            notification.setReceiver(question.getCreator());

            notification.setNotifiName(commentor.getName());
            notification.setOuterTitle(question.getTitle());
            notificationMapper.insert(notification);
        }else if (comment.getType() == CommentTypeEnum.BLOG.getType()){
            //回复博客
            Blog blog = blogMapper.queryBlogById(parentId);//根据评论parentId即blogId查询博客信息
            if (blog == null){
                throw new CustomerException(ErrorCode.BLOG_NOT_FOUND);
            }
            //添加评论
            commentMapper.insert(comment);
            blogMapper.updateCommentCount(parentId,1);
            //通知
            if (blog.getWriter() == comment.getCommenter()){
                //評論人和创建人同一人不通知
                return;
            }
            Notification notification = new Notification();
            notification.setGmtCreate(System.currentTimeMillis());
            notification.setType(NotificationTypeEnum.REPLY_BLOG.getType());
            notification.setOuterId(blog.getId());
            notification.setNotifier(comment.getCommenter());//评论人
            notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
            notification.setReceiver(blog.getWriter());//设置创建者

            notification.setNotifiName(commentor.getName());
            notification.setOuterTitle(blog.getTitle());
            notificationMapper.insert(notification);
        }else {
            //回复博客评论
            List<Comment> dbComment = commentMapper.queryCommentByParentId(parentId);
            if (dbComment.size() == 0){
                throw  new CustomerException(ErrorCode.COMMENT_NOT_FOUND);
            }
            //回复，添加信息
            commentMapper.insert(comment);
            //修改评论数，根据parentId
            commentMapper.updateCommentCount(parentId, 1);
            //通知
            Notification notification = new Notification();
            notification.setGmtCreate(System.currentTimeMillis());
            notification.setType(NotificationTypeEnum.REPLY_BLOG_COMMENT.getType());
            notification.setOuterId(parentId);
            notification.setNotifier(comment.getCommenter());
            notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
            Comment commenter = commentMapper.queryCommenter(comment.getGmtCreate());
            notification.setReceiver(commenter.getCommenter());
            notification.setNotifiName(commentor.getName());
            notification.setOuterTitle(comment.getContent());
            notificationMapper.insert(notification);
        }

    }
    //查询问题评论根据问题id和评论类型
    @Override
    public List<CommentDto> queryCommentByQuestionIdAndType(Integer id) {
        List<CommentDto> commentDtoList = new ArrayList<>();

            //执行查询问题评论
            List<Comment> comments =  commentMapper.queryAllCommentByParentIdAndType(id,CommentTypeEnum.QUESTION.getType());
            if (comments.size() == 0){
                //没有查询到数据
                return  new ArrayList<>();
            }
            //遍历集合
            for (Comment comment : comments) {
                //获取评论人
                User user = userMapper.queryUserById(comment.getCommenter());
                //将comment 对象转化为commentDto
                CommentDto commentDto = new CommentDto();
                BeanUtils.copyProperties(comment,commentDto);
                commentDto.setUser(user);
                commentDtoList.add(commentDto);
        }
        return commentDtoList;
    }
    @Override
    public List<CommentDto> queryQuestionCommentByQuestionIdAndType(Integer parentId) {
        List<CommentDto> commentDtoList = new ArrayList<>();
        //执行查询回复评论
        List<Comment> comments =  commentMapper.queryAllCommentByParentIdAndType(parentId,CommentTypeEnum.COMMENT.getType());
        if (comments.size() == 0){
            //没有查询到数据
            return  new ArrayList<>();
        }
        //遍历集合
        for (Comment comment : comments) {
            //获取评论人
            User user = userMapper.queryUserById(comment.getCommenter());
            //将comment 对象转化为commentDto
            CommentDto commentDto = new CommentDto();
            BeanUtils.copyProperties(comment,commentDto);
            commentDto.setUser(user);
            commentDtoList.add(commentDto);
        }
        return commentDtoList;
    }

    @Override
    public Comment  queryCommentByCommentId(Integer id) {
        Comment comment = commentMapper.queryCommentById(id);

        if (comment == null){
            throw  new CustomerException(ErrorCode.COMMENT_NOT_FOUND);
        }
        Integer parentId = comment.getParentId();
        return comment;
    }

    @Override
    public List<CommentDto> queryCommentByBlogIdAndType(Integer id) {
        List<CommentDto> commentDtoList = new ArrayList<>();

        //执行查询博客评论 type = 3
        List<Comment> comments =  commentMapper.queryAllCommentByParentIdAndType(id,CommentTypeEnum.BLOG.getType());
        if (comments.size() == 0){
            //没有查询到数据
            return  new ArrayList<>();
        }
        //遍历集合
        for (Comment comment : comments) {
            //获取评论人
            User user = userMapper.queryUserById(comment.getCommenter());
            //将comment 对象转化为commentDto
            CommentDto commentDto = new CommentDto();
            BeanUtils.copyProperties(comment,commentDto);
            commentDto.setUser(user);
            commentDtoList.add(commentDto);
        }
        return commentDtoList;
    }

    @Override
    public List<CommentDto> queryBlogCommentByBlogAndType(Integer parentId) {

        List<CommentDto> commentDtoList = new ArrayList<>();
        //执行查询微博回复 评论type = 4
        List<Comment> comments =  commentMapper.queryAllCommentByParentIdAndType(parentId,CommentTypeEnum.BLOG_COMMENT.getType());
        if (comments.size() == 0){
            //没有查询到数据
            return  new ArrayList<>();
        }
        //遍历集合
        for (Comment comment : comments) {
            //获取评论人
            User user = userMapper.queryUserById(comment.getCommenter());
            //将comment 对象转化为commentDto
            CommentDto commentDto = new CommentDto();
            BeanUtils.copyProperties(comment,commentDto);
            commentDto.setUser(user);
            commentDtoList.add(commentDto);
        }
        return commentDtoList;
    }
}
